package com.lhr.common.util;

import java.util.Collection;
import java.util.List;
import java.util.regex.Pattern;

import com.lhr.common.domain.Pagination;

public class ValidateUtil {
	/**
	 * 邮件地址
	 */
	private static Pattern PATTERN_EMAIL = Pattern.compile("^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$");
	/**
	 * 固定电话
	 */
	private static Pattern PATTERN_TEL = Pattern.compile("^([0-9]{3,4}-)?[0-9]{7,8}$");
	/**
	 * 移动电话
	 */
	private static Pattern PATTERN_MOBILE = Pattern.compile("^(\\+86)?0?1[3|5]\\d{9}$");
	/**
	 * 字母
	 */
	private static Pattern PATTERN_ALPHA = Pattern.compile("^[A-Za-z]+$");
	/**
	 * 数字
	 */
	private static Pattern PATTERN_DIGITAL = Pattern.compile("^\\d+$");
	/**
	 * 中文
	 */
	private static Pattern PATTERN_CHINESE = Pattern.compile("^[\\u4E00-\\u9FA5]+$");
	/**
	 * IP格式
	 */
	private static Pattern PATTERN_IP = Pattern.compile("^((00\\d|1?\\d?\\d|(2([0-4]\\d|5[0-5])))\\.){3}(00\\d|1?\\d?\\d|(2([0-4]\\d|5[0-5])))$");
	/**
	 * 重复字符格式
	 */
	private static Pattern PATTERN_REPEAT = Pattern.compile(".*(.).*\\1.*");

	public static boolean isEmpty(String str) {
		return (str == null || str.trim().length() < 1);
	}

	public static boolean isEmpty(List<?> list) {
		return (list == null || list.isEmpty());
	}

	public static boolean isEmpty(Collection<?> collection) {
		return (collection == null || collection.isEmpty());
	}

	public static boolean isEmpty(Object[] objs) {
		return (objs == null || objs.length < 1);
	}

	public static boolean isEmpty(Pagination<?> pagination) {
		return (pagination == null || pagination.getTotalCount() < 1);
	}

	public static boolean isNotEmpty(String str) {
		return isEmpty(str) ? false : true;
	}

	public static boolean isNotEmpty(List<?> list) {
		return isEmpty(list) ? false : true;
	}

	public static boolean isNotEmpty(Collection<?> collection) {
		return isEmpty(collection) ? false : true;
	}

	public static boolean isNotEmpty(Object[] objs) {
		return isEmpty(objs) ? false : true;
	}

	public static boolean isNotEmpty(Pagination<?> pagination) {
		return isEmpty(pagination) ? false : true;
	}

	/**
	 * 是否合法长度
	 */
	public static boolean isLegalityLength(String str,
			int minLength,
			int maxLength) {
		return (isEmpty(str) || str.length() < minLength || str.length() > maxLength) ? false : true;
	}

	/**
	 * 是否合法长度;最小长度为1
	 */
	public static boolean isLegalityLength(String str,
			int maxLength) {
		return isLegalityLength(str, 1, maxLength);
	}

	/**
	 * 邮件
	 */
	public static boolean isEmail(String email) {
		return (isEmpty(email) ? false : PATTERN_EMAIL.matcher(email).matches());
	}

	/**
	 * 固定电话
	 */
	public static boolean isTelephone(String telephone) {
		return (isEmpty(telephone) ? false : PATTERN_TEL.matcher(telephone).matches());
	}

	/**
	 * 移动电话
	 */
	public static boolean isMobilePhone(String mobilePhone) {
		return (isEmpty(mobilePhone) ? false : PATTERN_MOBILE.matcher(mobilePhone).matches());
	}

	/**
	 * 字母
	 */
	public static boolean isAlpha(String alpha) {
		return (isEmpty(alpha) ? false : PATTERN_ALPHA.matcher(alpha).matches());
	}

	/**
	 * 数字
	 */
	public static boolean isDigital(String digital) {
		return (isEmpty(digital) ? false : PATTERN_DIGITAL.matcher(digital).matches());
	}

	/**
	 * 中文
	 */
	public static boolean isChinese(String chinese) {
		return (isEmpty(chinese) ? false : PATTERN_CHINESE.matcher(chinese).matches());
	}

	/**
	 * 校验IP格式
	 */
	public static boolean isIP(String ip) {
		return (isEmpty(ip) ? false : PATTERN_IP.matcher(ip).matches());
	}

	/**
	 * 校验是否有重复字符
	 */
	public static boolean isHasRepeat(String repeat) {
		return (isEmpty(repeat) ? false : PATTERN_REPEAT.matcher(repeat).matches());
	}

}